package com.mobi.cacti.rabbit.controllers;

import java.io.ByteArrayInputStream;
import java.io.InputStream;

import android.content.Context;
import android.util.Log;

import com.mobi.cacti.rabbit.connectors.AbstractServerConnector;
import com.mobi.cacti.rabbit.connectors.ServerConnectorCallback;
import com.mobi.cacti.rabbit.connectors.TicketCountServerConnector;
import com.mobi.cacti.rabbit.response.Cache;
import com.mobi.cacti.rabbit.response.ReqTicketCount;
import com.mobi.cacti.rabbit.utils.SAXXMLParser;

public class TicketCountActivityController implements ServerConnectorCallback{
	
	private TicketCountServerConnector ticketCounterServerConnector;
	
	private ReqTicketCount[] ticketCountArray;
	
	public TicketCountActivityControllerCallback delegate;
	private static TicketCountActivityController instance = null;

	public static TicketCountActivityController getInstance() {
		if(instance == null) {
			instance = new TicketCountActivityController();
		}
		return instance;
	}

	public void loadTicketCount(Context context)
	{
		if(!Cache.isNetworkAvailable(context))
		{
			Cache.showNoInternetAlertDialog(context);
			return ;
		}
		
		String[] curcitArray = new String[Cache.reqMobileLogin.responseCircuitIDWhenLogin.keySet().size()];
		curcitArray = Cache.reqMobileLogin.responseCircuitIDWhenLogin.keySet().toArray(curcitArray);
		
//		Log.i("tai", "Count cirucit in array =" + curcitArray.length);
		
		ticketCountArray = new ReqTicketCount[curcitArray.length];
		
		for(int i = 0 ; i < curcitArray.length ; i++)
		{
			ticketCounterServerConnector = new TicketCountServerConnector();
			ticketCounterServerConnector.delegate = this;
			
			String cid = curcitArray[i];
			
//			Log.i("tai", "Load ticket count for =" + cid);
			
			ticketCounterServerConnector.loadTicketCountForCurcit(cid);
		}
	}
	
	private void handleReturnTicketCount(ReqTicketCount ticketCount)
	{
		//Add ticketCount to array
		for(int i = 0 ; i < ticketCountArray.length ; i++)
		{
			if(ticketCountArray[i] == null)
			{
				ticketCountArray[i] = ticketCount;
				break;
			}
		}
		
		boolean isContainNull = false;
		for(int i = 0 ; i < ticketCountArray.length ; i++)
		{
			if(ticketCountArray[i] == null)
			{
				isContainNull = true;
			}
		}
		
		if(isContainNull == false)
		{
			Cache.ticketCountArray = this.ticketCountArray;
			delegate.ticketCountActivityControllerCompleteCountTicket();
		}
	}

	@Override
	public void serverConnectorFinishBackgroundTask(
			AbstractServerConnector serverConnector, Object response) {
		
		if(serverConnector instanceof TicketCountServerConnector)
		{
			String requestCircuitID = ((String[]) response)[0];
			String responseData = ((String[]) response)[1];
			
//			Log.i("tai", "Ticket count = " + requestCircuitID + " >>> " + responseData);
			
			InputStream is = new ByteArrayInputStream(responseData.getBytes());
			ReqTicketCount obj = SAXXMLParser.respTicketCount(is);
			obj.curcitId = requestCircuitID;
			
//			Log.i("tai", "Ticket count = " + obj.curcitId + " >>> " + obj.ticketCount);
			
			this.handleReturnTicketCount(obj);
		}
		
	}
	
	
	
}
